﻿using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Util;
using Kingdee.BOS;
using System.ComponentModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Core.DynamicForm;
using System.Threading.Tasks;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.App;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
using Kingdee.BOS.Core.DynamicForm.Operation;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.Permission;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.App.Core.Warn.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.App.Data;


namespace erjibaojiadanshenghe
{
    [Description("二级报价单审核服务插件")]
    //热启动
    [HotUpdate]
    public class Class1 : AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            // 添加预加载字段
            e.FieldKeys.Add("BillNo");
        }
        public override void EndOperationTransaction(EndOperationTransactionArgs e)
        {
            base.EndOperationTransaction(e);
            foreach (DynamicObject item in e.DataEntitys)
            {
                var FBillNo = item["BillNo"].ToString();//编号
                //判断上游单据 并 获取FSId、、、
                string sql1 ="select T3.FSId,FSTableName,  FQTY AS  销售数量,FPRICEUNITQTY AS 计价数量,FPRICE AS 单价, " +
                             "    FTAXPRICE AS 含税单价,FTAXRATE AS '税率%', " +
                             "    FTAXAMOUNT AS 税额,FAMOUNT AS 金额,  FALLAMOUNT AS 价税合计, " +
                             "    FSTARTQTY AS 起订量,T2.FNOTE AS 备注, FMINGCHENG AS 名称 , FISFREE AS 是否赠品 ,FYUJIJIAOHUOSHIJIAN AS '预计交货时间（天）'" +
                             "    from VHTP_t_Cust100029 T1 " +
                             "    LEFT JOIN VHTP_t_Cust_Entry100059 T2 " +
                             "    ON T2.FID = T1.FID " +
                             "    LEFT JOIN VHTP_t_Cust_Entry100059_LK T3 " +
                             "    ON T3.FEntryID = T2.FEntryID " +
                             "    LEFT JOIN VHTP_t_Cust_Entry100059_F T4 " +
                             "    ON T4.FEntryID = T2.FEntryID " +
                             "    WHERE FSTableName = 'T_SAL_QUOTATIONENTRY' AND FBILLNO ='" + FBillNo + "' ";
                DataTable dt1;
                dt1 = DBUtils.ExecuteDataSet(this.Context, sql1).Tables[0];
                // 根据 FSId 反写上游单据 T_SAL_QUOTATIONENTRY T_SAL_QUOTATIONENTRY_F
                if (dt1.Rows.Count > 0)
                {
                    for (int i = 0; i < dt1.Rows.Count; i++)
                    {
                        String FSId = dt1.Rows[i][0].ToString();
                        String FQTY = dt1.Rows[i][2].ToString();
                        String FPRICEUNITQTY = dt1.Rows[i][3].ToString();
                        String FPRICE = dt1.Rows[i][4].ToString();
                        String FTAXPRICE = dt1.Rows[i][5].ToString();
                        String FTAXRATE = dt1.Rows[i][6].ToString();
                        String FTAXAMOUNT = dt1.Rows[i][7].ToString();
                        String FAMOUNT = dt1.Rows[i][8].ToString();
                        String FALLAMOUNT = dt1.Rows[i][9].ToString();
                        String FSTARTQTY = dt1.Rows[i][10].ToString();
                        String FNOTE = dt1.Rows[i][11].ToString();
                        String FMINGCHENG = dt1.Rows[i][12].ToString();
                        String FISFREE = dt1.Rows[i][13].ToString();
                        String FYUJIJIAOHUOSHIJIAN = dt1.Rows[i][14].ToString();
                        //反写 T_SAL_QUOTATIONENTRY  【FQTY、FSTARTQTY、FNOTE、FISFREE、FYUJIJIAOHUOSHIJIAN】
                        string update1 = "update T_SAL_QUOTATIONENTRY " +
                                         "set FQTY = '" + FQTY + "' ,FSTARTQTY='" + FSTARTQTY + "',FNOTE='" + FNOTE + "',FMINGCHENG='" + FMINGCHENG + "',FYUJIJIAOHUOSHIJIAN='" + FYUJIJIAOHUOSHIJIAN + "' " +
                                         "WHERE FENTRYID = '" + FSId + "' " +
                                         "";
                        DBUtils.ExecuteDynamicObject(this.Context, update1);
                        //反写 T_SAL_QUOTATIONENTRY_F 【FPRICEUNITQTY、FPRICE、FTAXPRICE、FTAXRATE、FTAXAMOUNT、FAMOUNT、FALLAMOUNT、FISFREE】
                        string update2 = " update T_SAL_QUOTATIONENTRY_F " +
                                         " set FPRICEUNITQTY = '" + FPRICEUNITQTY + "', FPRICE = '" + FPRICE + "', " +
                                         " FTAXPRICE = '" + FTAXPRICE + "', FTAXRATE = '" + FTAXRATE + "', " +
                                         " FTAXAMOUNT = '" + FTAXAMOUNT + "', FAMOUNT = '" + FAMOUNT + "', " +
                                         " FALLAMOUNT = '" + FALLAMOUNT + "',FISFREE = '" + FISFREE + "' " +
                                         " WHERE FENTRYID = '" + FSId + "'";
                        DBUtils.ExecuteDynamicObject(this.Context, update2);

                    }
                }

            }
        }
    }
}